package com.aizhixin.lab.project.template.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.template.domain.ModuleTaskDomain;
import com.aizhixin.lab.project.template.domain.ModuleTaskFileDomain;
import com.aizhixin.lab.project.template.service.ModuleTaskFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/api/v1/project/moduleTaskFile")
@RestController
@Api(tags = "任务模块", value = "任务文件管理")
public class ModuleTaskFileController {
    @Autowired
    private ModuleTaskFileService fileService;
    @Autowired
    private AccountService accountService;
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增", httpMethod = "POST", notes = "新增<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody ModuleTaskFileDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            fileService.save(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/delete", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除任务文件", httpMethod = "GET", notes = "删除任务文件<br><br><b>lwq</b>")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        result= fileService.delete(id);
        return new ResponseEntity(result, HttpStatus.OK);
    }
}
